package com.ftp;

import java.io.UnsupportedEncodingException;
import java.util.Properties;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

public class SFTPUtil {

	public static void upload(String host, int port, String username, String password, String src, String position, String fileName) throws Exception {
		Session sshSession = null;
		Channel channel = null;
		ChannelSftp sftp = null;

		JSch jsch = new JSch();
		sshSession = jsch.getSession(username, host, port);
		sshSession.setPassword(password);

		Properties sshConfig = new Properties();
		sshConfig.put("StrictHostKeyChecking", "no");
		sshSession.setConfig(sshConfig);
		
		sshSession.connect();
		System.out.println("session 连接");

		channel = sshSession.openChannel("sftp");
		channel.connect();

		sftp = (ChannelSftp)channel;
		System.out.println(String.format("成功连接到主机：%s，端口：%s", host, String.valueOf(port)));

		sftp.cd(position);
		sftp.put(src, fileName, ChannelSftp.OVERWRITE);
		sftp.quit();

		if(channel != null){
			channel.disconnect();
		}
		if(sshSession != null){
			sshSession.disconnect();
		}
	}

	public static void download(String host, int port, String username, String password, String src, String dst) throws Exception {
		Session sshSession = null;
		Channel channel = null;
		ChannelSftp sftp = null;

		JSch jsch = new JSch();
		sshSession = jsch.getSession(username, host, port);
		sshSession.setPassword(password);

		Properties sshConfig = new Properties();
		sshConfig.put("StrictHostKeyChecking", "no");
		sshSession.setConfig(sshConfig);
		
		sshSession.connect();
		System.out.println("session 连接");

		channel = sshSession.openChannel("sftp");
		channel.connect();

		sftp = (ChannelSftp)channel;
		System.out.println(String.format("成功连接到主机：%s，端口：%s", host, String.valueOf(port)));

		sftp.get(src, dst);
		sftp.quit();

		if(channel != null){
			channel.disconnect();
		}
		if(sshSession != null){
			sshSession.disconnect();
		}
	}
	
	public static void main(String[] args) throws UnsupportedEncodingException {
//		try {
//			SFTPUtil.upload("127.0.0.1", 29, "test", "test", "d:/temp/tt.txt", "E510001"+File.separator,"ddddddd.txt");
//			//SFTPUtil.download("127.0.0.1", 29, "test", "test", "t/ddddddd.txt", "d:/temp/test11111111111.txt");
//		} catch (Exception e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		String testStr1 = "A0010301019090                00000001290000001    022017120717204617120760119254      ERR093交易网代码错误                                                                                        000000            00000000000130302                20171207172046ERR093交易网代码错误                              0000000071    17120760119254      9090";
//		String s = null;
//		boolean b = "1".equals(s)?false:true;
//		System.out.println(b);
		//A0010301019090                00000001250000001    022017120610060717120660111426      000000成功                                                                                                000000            00000000000130302                20171206100607000000     成功                                      0000000031    17120660111426      909063&&
		String testStr =  "A0010301019090                00000001250000001    022017120610060717120660111426      000000成功                                                                                                000000            00000000000130302                20171206100607000000     成功                                      0000000031    17120660111426      9090";
		String testStr1 = "A0010301019090                00000001290000001    022017120717204617120760119254      8     客户信息错误                                                                                        000000            00000000000130302                201712071720468     客户信息错误                              0000000071    17120760119254      9090";
		int  a= testStr.getBytes("GBK").length;
		int  a1= testStr1.getBytes("GBK").length;
		System.out.println(a);
		System.out.println(a1);
	}
}
